import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token'),
    userInfo: JSON.parse(sessionStorage.getItem('userInfo')) // 从会话中获取
  },
  // set
  mutations: {
    // 设置 全局变量值  
    SET_TOKEN: (state, token) => {
      state.token = token;
      // 存储到 本地浏览器
      localStorage.setItem('token', token);
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
      // 存储到 本地浏览器
      sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
    },
    // 删除所有
    REMOVE_INFO: (state, token) => {
      state.token = '';
      state.userInfo = {};
      localStorage.setItem('token', '');
      sessionStorage.setItem('userInfo', JSON.stringify(''));
    }
  },
  // get
  getters: {
    getUser: state => {
      return state.userInfo;
    }
  },
  actions: {
  },
  modules: {
  }
})
